package com.hailou.service;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Properties;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.MappingJsonFactory;

import com.hailou.base.bean.Weather;
/**
 * @author hailou
 * @date 2013-12-9
 * @version 1.0
 */
public class WeatherReportService {

	public String getWeatherReport(String cityName){
		StringBuilder sb = new StringBuilder();		
		Weather weather = null;
		try {
			if(getCityCode(cityName)!=null){
				URL url = new URL("http://m.weather.com.cn/data/"+getCityCode(cityName)+".html");
				HttpURLConnection huc = (HttpURLConnection)url.openConnection();
				huc.setRequestMethod("GET");
				huc.setDoInput(true);
				InputStreamReader isr = new InputStreamReader(huc.getInputStream(),"UTF-8");
				BufferedReader br = new BufferedReader(isr);
				String temp = null;
				while((temp=br.readLine())!=null){
					sb.append(temp);
				}
				weather = getWeatherBean(sb.toString());
				isr.close();
				url =null;
				huc.disconnect();
				br = null;	
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return weather.toString();
	}
	
	public  Weather getWeatherBean(String jsonText) {  
        Weather weather = new Weather();  
        try {  
            JsonFactory jsonFactory = new MappingJsonFactory();  
            JsonParser jsonParser = jsonFactory.createJsonParser(jsonText);  
            jsonParser.nextToken();  
            HashMap<String, String> map = new HashMap<String, String>();  
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {  
                jsonParser.nextToken();  
                map.put(jsonParser.getCurrentName(), jsonParser.getText());  
            }  
            weather.setCity(map.get("city"));  
            weather.setDate(map.get("date_y"));  
            weather.setLunarDate(map.get("date"));  
            weather.setWeek(map.get("week"));  
            weather.setFcTime(map.get("fchh"));  
            weather.setTemperature(map.get("temp1"));  
            weather.setWeather(map.get("weather1"));  
            weather.setWind(map.get("wind1"));  
        } catch (Exception e) {  
            System.err.println(e);  
        }  
        return weather;  
    }  
	
	/**
	 * @param cityName
	 * @return
	 */
	public String getCityCode(String cityName) {
		//new WeatherReport().getWeatherReport("101110101");
		Properties properties = new Properties();
		String cityCode = null;
		try {
			InputStream in = WeatherReportService.class.getResourceAsStream("/weatherCityCode.properties");
			properties.load(in);
			cityCode = properties.getProperty(cityName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cityCode;
	}
	

	public static void main(String[] args) throws Exception {
		System.out.println(URLEncoder.encode("西安","gb2312"));
		//System.out.println(new WeatherReportService().getWeatherReport("北京"));
	}
}
